package com.markhsiu.result;

import com.alibaba.AAliServer;
import com.markhsiu.AppAsys;
import com.markhsiu.AppInstanceAsys;
import com.markhsiu.AppInterAsys;
import com.markhsiu.MachineAsys;
import com.markhsiu.bean.App;
import com.markhsiu.bean.Machine;
import com.markhsiu.tools.FileUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Mark Hsiu
 * @since 2018/8/2 上午11:47
 */
public class FirstFit {

    public static final List<String> result = new ArrayList<>();
    public static Map<String,Machine> machineMap = null;
    public static Map<String,App> appMap = null;

    public static Map<String,List<String>> appInstanceMap = null;
    public static Map<String,Map<String, Integer>> appInterMap = null;
    static {
        try {
            machineMap = MachineAsys.getMachines(AAliServer.PATH_MACHINE);
            appMap = AppAsys.getAppMap(AAliServer.PATH_APP);

            appInstanceMap = AppInstanceAsys.getInstanceMap(AAliServer.PATH_INSTANCE);
            appInterMap = AppInterAsys.getAppInterMap(AAliServer.PATH_APP_INTER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {

        for(Map.Entry<String, Machine> machineEntry : machineMap.entrySet()){
            Machine machine = machineEntry.getValue();
            for(String id : machine.idList){
                int num = 12;
                for(Map.Entry<String,List<String>> entry : appInstanceMap.entrySet()){
                    if(entry.getValue().size() == 0 ){
                        continue;
                    }
                    String instance = entry.getValue().remove(0);
                    result.add(instance+","+id);
                    num --;
                    if(num == 0){
                        break;
                    }
                }
            }
        }


        FileUtils.writeLines(AAliServer.PATH_RESULT,result);

    }


    public static boolean process(Machine machine){

        return true;
    }
}
